import { FairyEditor } from 'csharp';
import Global from './Global';
import UIPluginView from './fgui/view/UIPluginView';
import UrlUtils from './com/utils/UrlUtils';
import { genCode } from './GenCode_TS';

const App = FairyEditor.App;

console.log("--------->App.pluginManager.basePath:" + App.pluginManager.basePath );
console.log("__dirname: " +  eval("__dirname"));
//eval("__dirname") main.js 所在的位置
const CUR_PATH:string = eval("__dirname");
const Path_Project:string = CUR_PATH.substr( 0 , CUR_PATH.indexOf("/") );
const Path_Bin:string = Path_Project + "/bin/";
const Path_Res:string = Path_Bin + "/res/";
const Path_UI:string = Path_Res + "ui/";

console.log("--------->Path_Project: " +  Path_Project);
console.log("--------->Path_UI: " +  Path_UI);

UrlUtils.FAIRUI = App.pluginManager.basePath + "/" + Path_UI;
console.log("--------->FAIRUI: " +  UrlUtils.FAIRUI);

App.pluginManager.LoadUIPackage(UrlUtils.FAIRUI + 'Basic');
App.pluginManager.LoadUIPackage(UrlUtils.FAIRUI + 'common');
App.pluginManager.LoadUIPackage(UrlUtils.FAIRUI + 'plugin');

//https://zhuanlan.zhihu.com/p/196689870 委托


function onPublish(handler: FairyEditor.PublishHandler) {
    if (!handler.genCode) return;
    handler.genCode = false; //prevent default output

    console.log('Handling gen code in plugin');
    genCode(handler); //do it myself

    // console.log("xxx");
}

// class DemoInspector extends FairyEditor.View.PluginInspector {
//     // private combo: FairyGUI.GComboBox;
//     private btn_close:FairyGUI.GButton;

//     public constructor() {

//         super();

//         this.panel = FairyGUI.UIPackage.CreateObject("plugin", "UIPluginView").asCom;
//         this.btn_close = this.panel.GetChild("btn_close").asButton;
//         // this.combo = this.panel.GetChild("browser").asComboBox;
//         // this.combo.onChanged.Add(() => {
//         //     let obj = App.activeDoc.inspectingTarget
//         //     //use obj.docElement.SetProperty('xxx',..) instead of obj.xxx = ... to enable undo/redo mechanism
//         //     obj.docElement.SetProperty("customData", this.combo.value)
//         // });

//         this.btn_close.onClick.Add( ()=>{
//             App.groot.RemoveChild( this.panel );
//         } );

//         this.updateAction = () => { return this.updateUI(); };
//     }

//     private updateUI(): boolean {
//         let sels = App.activeDoc.inspectingTargets
//         let obj = sels.get_Item(0);

//         // this.combo.value = obj.customData

//         return true; //if everything is ok, return false to hide the inspector
//     }
// }

// function onDestroy() {
//     //do cleanup here
// }

//Register a inspector(检查员)
// App.inspectorView.AddInspector(() => new DemoInspector(), "DemoInspectorJS", "plugin");
// //Condition to show it
// App.docFactory.ConnectInspector("DemoInspectorJS", "mixed", false, false); 

App.menu.GetSubMenu( "help" ).AddItem( "text" , "test" , function(str):void{
    // console.log("text");
    if(Global.pulginView==null){
        Global.pulginView = new UIPluginView();
    }
    Global.pulginView.show(null);
    App.groot.AddChild( Global.pulginView );
} );

// export { onPublish, onDestroy };
